<?php
class BuildExamStructController extends ExtendedController
{
    function __construct()
    {
        parent::__construct();

        if(Session::getInstance()->get('permission') != '1')
        {
            $_SESSION['ref'] = substr($_SERVER['PATH_INFO'],1);
            Utility::error('Bạn không đủ quyền hạn để sử dụng chức năng này', 'login');
            exit();
        }
    }

    public function index($subjectID=null, $message = null)
    {
        $buildExamStructModel = $this->loadModel('buildexamstruct');
        if ($subjectID == null)
        {
            $id = (isset($_POST['id'])) ? $_POST['id'] : null;
            $name = (isset($_POST['name'])) ? $_POST['name'] : null;

            $rs = $buildExamStructModel->getSubjectList($id, $name);

            $subjectsView = $this->loadView('subjects');
            $subjectsView->setBlockData($rs);
            $subjectsView->appendBlockData(array('message'=>$message));
            $subjectsView->setBlockData("Chọn môn học","main_header");
            $subjectsView->render();
        }
        else
        {
            // retrive data from model
            $subject = $buildExamStructModel->getSubject($subjectID);
            $questionCount = $buildExamStructModel->getQuestionCount($subjectID);
            $sections = $buildExamStructModel->getSectionList($subjectID);

            // assign data to view
            $buildExamStructView =  $this->loadView('buildexamstruct');
            $buildExamStructView->appendBlockData(array('subject'=>$subject));
            $buildExamStructView->appendBlockData(array('questionCount'=>$questionCount));
            $buildExamStructView->appendBlockData(array('sections'=>$sections));
            $buildExamStructView->appendBlockData(array('errorMessage'=>$message));
            $buildExamStructView->setBlockData("Xây dựng cấu trúc đề thi","main_header");

            // render
            $buildExamStructView->render();
        }
    }
    
    public function save()
    {
        $subjectID = $_POST['subjectid'];
        $allQuestionCount = $_POST['allquestioncount'];
        $time = $_POST['time'];
        $examQuestionCount = $_POST['examquestioncount'];
        $weight1 = $_POST['weight1'];
        $weight2 = $_POST['weight2'];
        $weight3 = $_POST['weight3'];
        $weight4 = $_POST['weight4'];
        $qc = $_POST['qc']; // exam question counts array for every sections
        $sc = $_POST['sc']; // all question counts array for every sections
        $sid = $_POST['sid']; // IDs array for every sections
        
        $buildExamStructModel = $this->loadModel('buildexamstruct');
        $result = $buildExamStructModel->save($subjectID, $allQuestionCount, $time, $examQuestionCount, $weight1, $weight2, $weight3, $weight4, $qc, $sc, $sid);
        if ($result['check'])
            $this->index(null, $result['message']);
        else $this->index($subjectID, $result['message']);
    }
}
?>